package com.user_chat.chat.service.impl;

import com.user_chat.chat.bean.Auth;
import com.user_chat.chat.bean.Login;
import com.user_chat.chat.bean.Register;
import com.user_chat.chat.bean.User;
import com.user_chat.chat.bean.singlepage.UserInfor;
import com.user_chat.chat.mapper.AuthMapper;
import com.user_chat.chat.mapper.UserMapper;
import com.user_chat.chat.service.UserService;
import com.user_chat.chat.tools.JwtUtils;
import com.user_chat.chat.tools.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;


@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AuthMapper authMapper;

    @Autowired
    private JwtUtils jwtUtils;

    @Override
    public UserInfor getUserInfor(String username) {
        //1.获取用户信息
        User user = userMapper.selectOneByUsername(username);
        user.setPassword(null);

        //2.创建UserInfor
        UserInfor userInfor = new UserInfor();
        userInfor.setUser(user);

        return userInfor;
    }


    @Override
    public Login login(String username, String password) {

        Login login = new Login();

        //1.查询用户
        User user = userMapper.selectOneByUsername(username);

        //2.查不到用户，表示没有注册，直接返回
        if(user==null){
            login.setSuccess(false);
            login.setInfor("not register");
            return login;
        }

        //3.查到后，进行密码比对，如果不相等，表示登录失败
        if(!MD5Utils.encrypt(password).equals(user.getPassword())){
            login.setSuccess(false);
            login.setInfor("password error");
            return login;
        }

        //4.查到用户且密码比对相等，表示用户名密码都正确
        //根据该用户的身份，设置字段
        boolean admin = user.isAdmin();
        if(admin){
            //如果是管理员，
            login.setAdmin(true);
        }else {
            //如果不是管理，就是普通用户
            login.setAdmin(false);
        }
        login.setSuccess(true);
        login.setUsername(username);

        //5.生成token信息
        String token = jwtUtils.generateToken(login.getUsername(), login.isAdmin());
        login.setToken(token);

        //6.向redis存放token,过期时间为7天
        //redisTemplate.opsForValue().set(token,token,7, TimeUnit.DAYS);

        //7.返回login
        return login;

    }


    @Override
    public boolean checkHasRegister(String username) {
        return userMapper.selectOneByUsername(username)!=null;
    }

    @Override
    public Register register(String username, String password, String authCode, String phoneNumber){

        User user = new User();
        Register register = new Register();

        //1.检查注册过没
        boolean hasRegister = this.checkHasRegister(username);
        if(hasRegister){
            //查到了，说明已经注册过了，返回false注册失败
            register.setSuccess(false);
            register.setInfor("hasRegister");
            return register;
        }

        //2.查看授权码是否为空,如果为空，则表示是普通用户
        if(authCode!=null){
            //3.不为空，则核对授权码是否正确，不正确就直接返回
            Auth auth = authMapper.selectOneByCode(authCode);
            if(auth==null){//auth为空，表示不存在，即授权码不正确
                register.setInfor("authCode not correct");
                register.setSuccess(false);
                return register;
            }
            //不为空，授权码正确，标记为管理员
            user.setAdmin(true);
        }
        //4.创建对象
        user.setUsername(username);
        user.setPassword(password);
        user.setRegisterTime(LocalDateTime.now());

        //5.先检查是否注册过
        //User userByCheck = userMapper.selectOneByUsername(user.getUsername());


        //6.没查到即没有注册过，进行注册,注册前加密密码
        user.setPassword(MD5Utils.encrypt(user.getPassword()));
        int count = userMapper.insertOne(user);
        //7.如果count>0，表示注册失败
        if(count==0){
            register.setSuccess(false);
            register.setInfor("system error");
            return register;
        }
        //8.注册成功
        register.setSuccess(true);

        return register;
    }
}
